package com.github.zxhtom.excel.expose;

import com.github.zxhtom.excel.Excel;
import com.github.zxhtom.excel.constant.HeadConstant;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @package com.github.zxhtom.excel.expose
 * @Class PoiTools
 * @Description TODO
 * @Author zhangxinhua
 * @Date 19-11-6 下午3:01
 */
public class PoiTools<T> implements Excel<T> {
    @Override
    public void readData() {

    }

    @Override
    public void export(List<T> ts) {
        String parentFileName = EasyExcelTools.class.getResource("/").getPath() + "student/poi/";
        File file = new File(parentFileName);
        if (!file.exists()) {
            file.mkdirs();
        }
        String fileName = parentFileName + System.currentTimeMillis() + ".xlsx";
        Integer currentRowIndex = 0;
        String head[] = new String[]{HeadConstant.FIRSTNAME,HeadConstant.SECONDNAME};
        String sixHead[] = new String[]{"学号","姓名","年龄","性别","生日","身高"};
        String sixField[] = new String[]{"id","userName","age","sex","birth","height"};
        //构建workbook工作部
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        //建立sheet页
        HSSFSheet sheet = workbook.createSheet("中化安元");
        //前两行为表头，且需要合并单元格
        for (String headName : head) {
            HSSFRow row = sheet.createRow(currentRowIndex);
            Integer currentColIndex = 0;
            HSSFCell cell = row.createCell(currentColIndex);
            cell.setCellValue(headName);
            cell.setCellStyle(cellStyle);
            //这里需要合并单元格。但是跨长度未知 。 这里我们必须提前知道长度=6
            sheet.addMergedRegion(new CellRangeAddress(currentRowIndex, currentRowIndex, currentColIndex, sixHead.length-1));
            currentRowIndex++;
        }
        //第三行是6个表头
        HSSFRow headRow = sheet.createRow(currentRowIndex++);
        for (int i = 0; i < sixHead.length; i++) {
            HSSFCell cell = headRow.createCell(i);
            cell.setCellValue(sixHead[i]);
        }
        //开始生成主体数据
        for (T t : ts) {
            HSSFRow dataRow = sheet.createRow(currentRowIndex++);
            Class<?> clazz = t.getClass();
            for (int i = 0; i < sixField.length; i++) {
                try {
                    Field field = clazz.getDeclaredField(sixField[i]);
                    field.setAccessible(true);
                    Object o = field.get(t);
                    HSSFCell cell = dataRow.createCell(i);
                    cell.setCellValue(o.toString());
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        File excelFile = new File(fileName);
        try {
            workbook.write(excelFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
